package com.fs.service.impl;


import com.fs.dao.PaperDao;
import com.fs.dao.QuestionAnswsersDao;
import com.fs.dao.QuestionTypeDao;
import com.fs.dao.QuestionsDao;
import com.fs.pojo.Paper;
import com.fs.pojo.QuestionAnswsers;
import com.fs.pojo.QuestionType;
import com.fs.pojo.Questions;
import com.fs.service.QuestionsService;
import com.fs.utils.BaseContext;
import com.fs.utils.BeanFactories;
import com.fs.vo.QuestionVO;
import net.sf.cglib.beans.BeanCopier;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (TbQuestions)表服务实现类
 *
 * @author makejava
 * @since 2022-11-15 10:21:17
 */

public class QuestionsServiceImpl implements QuestionsService {

    private QuestionsDao questionsDao = BeanFactories.getBean(QuestionsDao.class);
    private QuestionTypeDao questionTypeDao = BeanFactories.getBean(QuestionTypeDao.class);
    private QuestionAnswsersDao questionAnswsersDao = BeanFactories.getBean(QuestionAnswsersDao.class);
    private PaperDao paperDao = BeanFactories.getBean(PaperDao.class);


    @Override
    public List<QuestionVO> queryAllQuestionsByPaperIdnClassPaperId(String paperId,String classPaperId) {

        List<Questions> questions = questionsDao.queryAllQuestionsByPaperId(paperId);

        List<QuestionVO> vos = null;
        if(questions != null) {
            vos = questions.stream().map(question -> {
                QuestionVO vo = new QuestionVO();

                QuestionType questionType = questionTypeDao.queryById(question.getType());

                QuestionAnswsers questionAnswsers = questionAnswsersDao.queryStudentAnswer(BaseContext.getUserId(), classPaperId, question.getId());
                vo.setStudentAnswsers(questionAnswsers);

                BeanCopier.create(QuestionType.class, QuestionVO.class, false).copy(questionType, vo, null);
                BeanCopier.create(Questions.class, QuestionVO.class, false).copy(question, vo, null);

                return vo;
            }).collect(Collectors.toList());
        }



        return vos;
    }

    @Override
    public List<Questions> getAllQuestionsByPaperId(String paperId) {
        List<Questions> questionsList = questionsDao.queryAllQuestionsByPaperId(paperId);//查询该试卷的所有试题

        return questionsList;
    }
}
